var subjectCategoryType;
var id;
var groupid;
var iconid;
var subid;
var menuid;

$(function(){
   	 groupid = ly.getUrlParam("groupid");
	 iconid = ly.getUrlParam("iconid");
	 subid = ly.getUrlParam("subid");
	 menuid = ly.getUrlParam("menuid");	

	bannerType = ly.getUrlParam("bannerType");
	id = ly.getUrlParam("id");
	if(bannerType == 2){
		$("#title").html("编辑");
		  $(".form").getForm({
	        id: id
	   },function (data) {
	    	$("#bannerType").getSelect({
				type:"radio",
				asName:"bannerType",
				code:"bannerType",
				default:data.bannerType
			});
			
			$("#bannerUrlType").getSelect({
				type:"radio",
				asName:"bannerUrlType",
				list:[
					{value:"1",display:"商品管理"},
					{value:"2",display:"景点管理"},
					{value:"3",display:"新闻资讯"},
					{value:"4",display:"商家管理"},
					{value:"5",display:"游记攻略"},
					{value:"6",display:"其他"}
				],
				default:data.bannerUrlType		
			});
			if(data.businessId != null && data.businessId !=''){
				selectbusinessList(data.bannerUrlType,data.businessId);
			}
			
	    });
	}else{
		$("#bannerType").getSelect({
				type:"radio",
				asName:"bannerType",
				code:"bannerType",
				default:'1'
			});
			
			$("#bannerUrlType").getSelect({
				type:"radio",
				asName:"bannerUrlType",
				list:[
					{value:"1",display:"商品管理"},
					{value:"2",display:"景点管理"},
					{value:"3",display:"新闻资讯"},
					{value:"4",display:"商家管理"},
					{value:"5",display:"游记攻略"},
					{value:"6",display:"其他"}
				],
				default:6			
			});
		}
	
	
	//获取到选择的值
	$("input[name=bannerUrl]").on("click",function(){
		console.log(this.value);
	});
	
	//提交
    $("body").on("click","#save-btn-commodityClassification",function(event){
        event.preventDefault();
        var success =  $('.form').validate('submitValidate');
        if(success){
            //验证通过提交表单
            submitFom();
        }
    })
	
	//批量删除
	$("#batchDelBtn").click(function(){
		var c = "";
		$("input[name=datachk]:checked").each(function(){
			c += $(this).val()+',';
		});
		deleteFirstCategory(c);
	});
	
	$("body").on("click","input[name='bannerUrlType']",function(){
		var bannerUrl = $(this).val();
		$("#bannerUrl").val("");
		selectbusinessList(bannerUrl,'');
	});
	
});

/*
 * 提交表单
 */
function submitFom(){
    var bannerUrl =$("#bannerUrl").val();
 	var bannerDesc =$("#bannerDesc").val();
 	var sortOrder = $("#sortOrder").val();
 	var bannerTypeBack = $("#bannerType input:checked").val();
 	
 	var bannerUrlType = $("#bannerUrlType input:checked").val();
 	var businessId = $('select[name=businessId] option:checked').val();
 	
 	var fileName = $('#bPicUrl input').attr('filename');
    var filePath = $('#bPicUrl input').attr('value');
    var fileType = $('#bPicUrl input').attr('filetype');
    var fileSize = $('#bPicUrl input').attr('filesize');
 	
  	var obj = {};
  	var url = site.interUrl+"/banner/insertOrUpdateBanner";
  	/*var f = {//图标
		'fileName':fileName,
		'filePath':filePath,
		//'fileType':fileType,
		'fileSize':fileSize
    };*/
    var fileuploadList = new Array();
 	var files = $('#bPicUrl div input.filepath');
 	files.each(function() {
		var o = {
			'fileName': $(this).attr("filename"),
			//'fielType':$(this).attr("filetype"),
			'fileSize': $(this).attr("filesize"),
			'filePath': $(this).val()
		};
		fileuploadList.push(o);
	})
  	if(bannerType == 1){
		//新增
		 obj = {
	    	'bannerUrl':bannerUrl,
	    	'bannerDesc':bannerDesc,
	    	'sortOrder':sortOrder,
	    	'bannerType':bannerTypeBack,
	    	'bannerUrlType':bannerUrlType,
	    	'businessId':businessId
	    };
  	}else{
  		//修改
  		var thisId = $("#bannerId").val();
  		obj = {
  			'bannerUrl':bannerUrl,
	    	'bannerDesc':bannerDesc,
	    	'sortOrder':sortOrder,
	    	'bannerType':bannerTypeBack,
	    	'bannerUrlType':bannerUrlType,
	    	'businessId':businessId,
  			'id':thisId
  		};
  	}
  	obj.listFileupload = fileuploadList;
  	console.log(obj);
	request({
		url:url,
		data:obj,
		type:"post",
		ajaxform:'true',
		callback:function(f) {
			console.log(f);
			if(f.code=="rest.success"){
				if(bannerType == 1){
					ly.alert('保存成功',function(){	            
			    		ly.goto("banner.html");
			        });
				}else{
					ly.alert('修改成功',function(){	           
			    		ly.goto("banner.html");
			        });
				}
			}else{
				ly.toast(f.desc);
			}
		}
	});
  	
}

function selectbusinessList(bannerUrlType,businessId){
	var list = new Array();
	$("#businessId").empty();
	$("#changBusinessId").addClass("hide");
	$("#changeBannerUrl").addClass("hide");
	if(bannerUrlType==1){
		//商品管理
		request({
			url:site.interUrl+"/merchantProducts/selectMerchantProductsList",
			data:{'num':1,'size':100,'isUsing':0},
			type:"get",
			callback:function(f) {
				console.log(f);
				if(f.code=="rest.success"){
					var obj = {value:' ',display:'请选择'};
					list.push(obj);
					if(f.result.list!= null && f.result.list !=''){
						obj = {};
						$.each(f.result.list,function(i,o){
							obj = {value:o.id,display:o.productsName};
							list.push(obj);
						});
					}
					$("#businessId").getSelect({
						type: "select",
						asName: "businessId",
						list:list,
						default:businessId
					});
				}
			}
		});	
		$("#changBusinessId").removeClass("hide");
	}else if(bannerUrlType==2){
		//景点
		request({
			url:site.interUrl+"/scenicSpot/selectScenicSpotList",
			data:{'num':1,'size':100,'isUsing':0},
			type:"get",
			callback:function(f) {
				console.log(f);
				if(f.code=="rest.success"){
					var obj = {value:' ',display:'请选择'};
					list.push(obj);
					if(f.result.list!= null && f.result.list !=''){
						obj = {};
						$.each(f.result.list,function(i,o){
							obj = {value:o.id,display:o.spotName};
							list.push(obj);
						});
					}
					$("#businessId").getSelect({
						type: "select",
						asName: "businessId",
						list:list,
						default:businessId
					});
				}
			}
		});	
		$("#changBusinessId").removeClass("hide");
	}else if(bannerUrlType == 3){
		//新闻资讯
		request({
			url:site.interUrl+"/new/selectNew",
			data:{'num':1,'size':100},
			type:"get",
			callback:function(f) {
				console.log(f);
				if(f.code=="rest.success"){
					var obj = {value:' ',display:'请选择'};
					list.push(obj);
					if(f.result.list!= null && f.result.list !=''){
						obj = {};
						$.each(f.result.list,function(i,o){
							obj = {value:o.id,display:o.newName};
							list.push(obj);
						});
					}
					$("#businessId").getSelect({
						type: "select",
						asName: "businessId",
						list:list,
						default:businessId
					});
				}
			}
		});	
		$("#changBusinessId").removeClass("hide");
	}else if(bannerUrlType == 4){
		//商家管理
		request({
			url:site.interUrl+"/business/selectBusinessList",
			data:{'num':1,'size':100,},
			type:"get",
			callback:function(f) {
				console.log(f);
				if(f.code=="rest.success"){
					var obj = {value:' ',display:'请选择'};
					list.push(obj);
					if(f.result.list!= null && f.result.list !=''){
						obj = {};
						$.each(f.result.list,function(i,o){
							obj = {value:o.id,display:o.businessName};
							list.push(obj);
						});
					}
					$("#businessId").getSelect({
						type: "select",
						asName: "businessId",
						list:list,
						default:businessId
					});
				}
			}
		});	
		$("#changBusinessId").removeClass("hide");
	}else if(bannerUrlType == 5){
		//游记
		request({
			url:site.interUrl+"/travels/selectTravelsList",
			data:{'num':1,'size':100},
			type:"get",
			callback:function(f) {
				console.log(f);
				if(f.code=="rest.success"){
					var obj = {value:' ',display:'请选择'};
					list.push(obj);
					if(f.result.list!= null && f.result.list !=''){
						obj = {};
						$.each(f.result.list,function(i,o){
							obj = {value:o.id,display:o.travelsName};
							list.push(obj);
						});
					}
					$("#businessId").getSelect({
						type: "select",
						asName: "businessId",
						list:list,
						default:businessId
					});
				}
			}
		});	
		$("#changBusinessId").removeClass("hide");
	}else{
		$("#changeBannerUrl").removeClass("hide");
	}
}